const axios = require('axios')
const { assign } = require('lodash')

const instance = axios.create({
  timeout: 60000
})

const request = async option => {
  const datas = await instance(option)
  // console.log('request results:', datas)
  return datas
}

const get = async (url, config) => {
  // 所有的get请求都加上时间戳，防止IE11缓存
  let timestamp = new Date().getTime()
  if (/.*\?.*$/.test(url)) {
    url += '&r=' + timestamp
  } else {
    url += '?r=' + timestamp
  }
  let datas = await request(
    assign(
      {
        url
      },
      config
    )
  )

  return datas
}

const post = async (url, data, config) => {
  let datas = await request(
    assign(
      {
        url,
        method: 'post',
        data: data
      },
      config
    )
  )
  return datas
}

module.exports = { request, get, post }
